package com.djz.stuinfomge.util;

import java.util.ArrayList;
import java.util.List;



import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
 * JPQL语句分页查询
 * 
 * @author liufang
 * 
 */
public class Finder {
    protected Finder() {
    }

    public Finder(String ql) {
	qlBuilder = new StringBuilder(ql);
    }

    public static Finder create(String ql) {
	Finder finder = new Finder(ql);
	return finder;
    }

    public Finder append(String ql) {
	qlBuilder.append(" "+ql+" ");
	return this;
    }

    /**
     * 获得原始ql语句
     * 
     * @return
     */
    public String getOrigQl() {
	String ql = qlBuilder.toString();
	logger.debug("----------------------- ql语句："+ql);
	return ql;
    }

    /**
     * 获得查询数据库记录数的ql语句。
     * 
     * @return
     */
    public String getRowCountQl() {
	String ql = qlBuilder.toString();
	
	String rowCountQl = ql.replace(HQL_FETCH, "");

	int index = rowCountQl.toLowerCase().indexOf(ORDER_BY);
	if (index > 0) {
	    rowCountQl = rowCountQl.substring(0, index);
	}
	
	//去掉group by
	int groupByIndex = rowCountQl.toLowerCase().indexOf("group by");
	if (groupByIndex > 0) {
	    rowCountQl = rowCountQl.substring(0, groupByIndex);
	}
	int fromIndex = rowCountQl.toLowerCase().indexOf("from");
	//from前有select，去掉select
	if(fromIndex > 0){
	    rowCountQl = rowCountQl.substring(fromIndex, rowCountQl.length());
	}
	 
	
	
	
	ql = ROW_COUNT + rowCountQl;
	return ql;
    }

    
    public Finder setParam(String param, Object value) {
	getParams().add(param);
	getValues().add(value);

	return this;
    }

  

    /**
     * 将finder中的参数设置到query中。
     * 
     * @param query
     */
    public Query setParamsToQuery(Query query) {
	//logger.debug("--------------params is null: "+( params == null));
	if (params != null) {
	    for (int i = 0; i < params.size(); i++) {
		logger.debug("--------------------param name："+ params.get(i)+" param value: "+ values.get(i));
		query.setParameter(params.get(i), values.get(i));
	    }
	}
	return query;
    }

    public Query createQuery(Session session) {
	return setParamsToQuery(session.createQuery(getOrigQl()));
    }

   

    private List<String> getParams() {
	if (params == null) {
	    params = new ArrayList<String>();
	}
	return params;
    }

    private List<Object> getValues() {
	if (values == null) {
	    values = new ArrayList<Object>();
	}
	return values;
    }

    private StringBuilder qlBuilder;

    private List<String> params;
    private List<Object> values;

    public static final String ROW_COUNT = "select count(*) ";
    public static final String FROM = "from";
    public static final String DISTINCT = "distinct";
    public static final String HQL_FETCH = "fetch";
    public static final String ORDER_BY = "order";

    @Test
    public void test() {
	Finder find = Finder
		.create("select distinct p FROM BookType join fetch p");
	System.out.println(find.getRowCountQl());
	System.out.println(find.getOrigQl());
    }
    private Logger logger = LoggerFactory.getLogger(getClass());
}
